require 'json'
require_relative '../../../app/models/utils/upload/action_upload'
class Admin::ArticlesController < AdminApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy]

  # GET /admin/articles
  # GET /admin/articles.json
  def index
    @articles = Article.where(:is_deleted => 0).paginate(:page => params[:page])
  end

  def search
    @articles = Article.joins('left outer join article_categories c on c.article_id = articles.id left outer join article_tags at on at.article_id = articles.id').where('articles.is_deleted' => 0)
    if !search_article_params.to_h['category_id'].nil?
      @articles = @articles.where('c.category_id'=> search_article_params.to_h['category_id'])
      @category_ids = search_article_params.to_h['category_id']
    end
    if !search_article_params.to_h['tag_id'].nil?
      @articles = @articles.where('at.id'=> search_article_params.to_h['tag_id'])
      @tag_ids = search_article_params.to_h['tag_id']
    end
    @articles = @articles.uniq.paginate(:page => params[:page])
    render 'admin/articles/index'
  end

  # GET /admin/articles/1
  # GET /admin/articles/1.json
  def show
  end

  # GET /admin/articles/new
  def new
    @article = Article.new
  end

  # GET /admin/articles/1/edit
  def edit
  end

  # POST /admin/articles
  # POST /admin/articles.json
  def create
    @article = Article.new(article_params)
    respond_to do |format|
      if @article.save
        format.html { redirect_to admin_article_path(@article), notice: 'Article was successfully created.' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /admin/articles/1
  # PATCH/PUT /admin/articles/1.json
  def update
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to admin_article_path(@article), notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    @article.delete
    respond_to do |format|
      format.html { redirect_to admin_articles_url, notice: 'Article was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def ueditor
    action_type = params[:action_type].to_sym
    config = JSON.parse(File.read(__dir__ + '/../../assets/ueditor/config.json'))
    case action_type
      when :config
        result = config
      when :uploadimage,:uploadscrawl,:uploadvideo,:uploadfile
        result = ActionUpload.uploadconfig(config,action_type,params)
      when :listimage,:listfile

      when :catchimage

      else
        result = {:state => '请求地址出错'}
    end
    render json: result
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.required(:article).permit(:title,:content,category_ids:[],tag_ids: [])
    end

    def search_article_params
      params.permit(category_id:[],tag_id: [])
    end
end
